package com.bitbakery.plugin.lisp.navigation;

import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class LispClassChooser extends LispChooser {

    public String[] getNames(Project project, boolean includeNonProjectItems) {
        // TODO - Get list of defuns and defmacros (anything else?)
        List<String> names = gatherNames(project.getProjectFile().getParent());
        return names.toArray(new String[names.size()]);
    }

    public NavigationItem[] getItemsByName(final String name, Project project, boolean includeNonProjectItems) {
        //final VirtualFile file = project.getProjectFile().getParent().findChild(name);
        return new NavigationItem[]{
                new NavigationItem() {
                    @Nullable
                    public String getName() {
                        return name;
                    }

                    @Nullable
                    public ItemPresentation getPresentation() {
                        return new ItemPresentation() {
                            public String getPresentableText() {
                                return name;
                            }

                            @Nullable
                            public String getLocationString() {
                                return name;
                            }

                            @Nullable
                            public Icon getIcon(boolean open) {
                                return null;
                            }

                            @Nullable
                            public TextAttributesKey getTextAttributesKey() {
                                return null;
                            }
                        };  //To change body of implemented methods use File | Settings | File Templates.
                    }

                    public FileStatus getFileStatus() {
                        return null;
                    }

                    public void navigate(boolean requestFocus) {
                        // TODO - Open the file and go to the defun, defmacro or whatever...
                    }

                    public boolean canNavigate() {
                        return true;
                    }

                    public boolean canNavigateToSource() {
                        return true;
                    }
                }
        };
    }

    private List<String> gatherNames(VirtualFile directory) {
        List<String> names = new ArrayList<String>();
        if (directory != null) {
            for (VirtualFile file : directory.getChildren()) {
                if (file.isDirectory()) {
                    // TODO - We actually need to get the PSI from th efile to get the defuns, etc.
                    names.addAll(gatherNames(file));
                } else if (isLisp(file)) {
                    names.add(file.getName());
                }
            }
        }
        return names;
    }

    private boolean isLisp(VirtualFile file) {
        String ext = file.getExtension();
        return "lisp".equalsIgnoreCase(ext) || "lisp".equalsIgnoreCase(ext);
    }
}
